// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters;

import java.util.ArrayList;
import java.util.Comparator;
import zombie.iso.IsoUtils;

public abstract class BaseZombieSoundManager {
    protected final ArrayList<IsoZombie> characters = new ArrayList();
    private final long[] soundTime;
    private final int staleSlotMS;
    private final Comparator<IsoZombie> comp = new Comparator<IsoZombie>() {
        public int compare(IsoZombie isoZombie0, IsoZombie isoZombie1) {
            float float0 = BaseZombieSoundManager.this.getClosestListener(isoZombie0.x, isoZombie0.y, isoZombie0.z);
            float float1 = BaseZombieSoundManager.this.getClosestListener(isoZombie1.x, isoZombie1.y, isoZombie1.z);
            if (float0 > float1) {
                return 1;
            } else {
                return float0 < float1 ? -1 : 0;
            }
        }
    };

    public BaseZombieSoundManager(int numSlots, int _staleSlotMS) {
        this.soundTime = new long[numSlots];
        this.staleSlotMS = _staleSlotMS;
    }

    public void addCharacter(IsoZombie chr) {
        if (!this.characters.contains(chr)) {
            this.characters.add(chr);
        }
    }

    public void update() {
        if (!this.characters.isEmpty()) {
            this.characters.sort(this.comp);
            long _long = System.currentTimeMillis();

            for (int int0 = 0; int0 < this.soundTime.length && int0 < this.characters.size(); int0++) {
                IsoZombie isoZombie = (IsoZombie)this.characters.get(int0);
                if (isoZombie.getCurrentSquare() != null) {
                    int int1 = this.getFreeSoundSlot(_long);
                    if (int1 == -1) {
                        break;
                    }

                    this.playSound(isoZombie);
                    this.soundTime[int1] = _long;
                }
            }

            this.postUpdate();
            this.characters.clear();
        }
    }

    public abstract void playSound(IsoZombie chr);

    public abstract void postUpdate();

    private float getClosestListener(float float5, float float6, float float7) {
        float float0 = Float.MAX_VALUE;

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer = IsoPlayer.players[_int];
            if (isoPlayer != null && isoPlayer.getCurrentSquare() != null) {
                float float1 = isoPlayer.getX();
                float float2 = isoPlayer.getY();
                float float3 = isoPlayer.getZ();
                float float4 = IsoUtils.DistanceToSquared(float1, float2, float3 * 3.0F, float5, float6, float7 * 3.0F);
                if (isoPlayer.Traits.HardOfHearing.isSet()) {
                    float4 *= 4.5F;
                }

                if (float4 < float0) {
                    float0 = float4;
                }
            }
        }

        return float0;
    }

    private int getFreeSoundSlot(long long1) {
        long long0 = Long.MAX_VALUE;
        int int0 = -1;

        for (int int1 = 0; int1 < this.soundTime.length; int1++) {
            if (this.soundTime[int1] < long0) {
                long0 = this.soundTime[int1];
                int0 = int1;
            }
        }

        return long1 - long0 < (long)this.staleSlotMS ? -1 : int0;
    }
}
